import requests
import json
import csv
from matplotlib import pyplot as plt
for i in range(3):
    url='https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=jQuery34109620444653172955_1589816998769&_=1589816998770'
    headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
            'cookie': 'pgv_pvi=7399261184; LW_uid=619508s7d6P3L0k3I6x0R5b5e3; eas_sid=91U578R7B633t0O3Q6B0Q5t6J1; pgv_pvid=5482852386; RK=PKzNUF3S9e; ptcz=6136657377c9f36346afabd89c748b94b2591cd2c85fc89917b14d03f0c63e79; luin=o2497744746; lskey=000100004f73dd537a49eb575fe3fd3ca80428f20b72b7386829e5d39f62f254f102f6b9342961e317118777; tvfe_boss_uuid=702c6a60341cb84d; LW_sid=R1i5g8c7p7V8B8U7t6e3r1b6J6; o_cookie=2497744746; same_pc=%5B%7B%22uin%22%3A%227947409EF8B37ABEDC7698C8C8FA129E%22%2C%22qnid%22%3A11475%2C%22status%22%3A1%7D%5D'
        }
    response=requests.get(url,headers=headers)
    response.encoding=response.apparent_encoding
    data=response.text
    datas=data[40:]
    data1=datas[1:]
    data2=data1[:-1]
    a=json.loads(data2)
    countries=a['data']
    # pprint.pprint(a)
    # pprint.pprint(countries)
    b=json.loads(countries)
    countrie_list=b['areaTree']
    # print(len(countrie_list))
    for country in countrie_list:
        country_datas=country['children']
    # print(country_datas)

country_list=[]

for info in country_datas:
    country_dict = {}
    country_dict['国家']=info['name']
    nowConfirm=info['total']['nowConfirm']
    country_dict['确诊人数']=info['total']['confirm']
    country_list.append(country_dict)
    # print(country_list)
    # plt.plot_date(info['name'], info['total']['confirm'], linestyle='--', color='red', marker='o')

# plt.show()

sheet_title=country_list[0].keys()

sheet_data=[]

csv_fp=open('中国实时疫情.csv','w')
for i in country_list:
    sheet_data.append(i.values())
    print(i)
    # plt.plot_date(sheet_title, i.values(), linestyle='--', color='red', marker='o')
writer=csv.writer(csv_fp)
writer.writerow(sheet_title)
writer.writerows(sheet_data)
csv_fp.close()

# plt.plot_date(info['name'],info['name'],linestyle='--',color='red',marker='o')
# plt.show()



